<template>
  <div class="maintenanceStandard flowCloumn h100">
    <div class="constentmain h100">
      <list-table-new v-bind="tableSetting" ref="klTable"> </list-table-new>
    </div>
    <dialogPublic
      :title="'导入文件'"
      :show.sync="show"
      width="40%"
      :noFootBtn="true"
    >
      <div style="text-align: left; height: 200px" v-if="show">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-change="handleChange"
          :multiple="false"
          :auto-upload="false"
          accept=".xls,.xlsx"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div class="el-upload__tip" slot="tip">
            只能上传excle文件，且不超过500kb
          </div>
        </el-upload>
        <el-button v-if="path != ''" type="text" @click="downLoadResult"
          >下载错误备注文件</el-button
        >
      </div>
    </dialogPublic>
  </div>
</template>
<script>
import {
  queryClassificAndSubList,
  setCommonClassificAndSub,
  importClassificAndSubStandard,
} from "@/api/standardManagement/classificationAndSub.js";
export default {
  data() {
    return {
      pageType: "list",
      checked: false,
      show: false,
      path: "",
      searchData: {},
      uploadUrl: `${process.env.VUE_APP_BASEURL}/eam-service-standard/grade/import`,
      tableSetting: {
        selectionShow: true,
        pageConfig: {
          pageCode: "classificationAndSub",
          userId: this.$store.getters.userInfo.namePy,
        },
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
            publicStatus: 1,
            appid: this.$store.state.user.userInfo.appId,
          },
          searchArr: [
            {
              type: "input",
              label: "标题",
              model: "name",
              placeholder: "请输入",
            },
            {
              type: "organization",
              label: "创建单位",
              model: "deptName",
              idsStr: "deptId",
            },
            {
              type: "deviceClassify",
              label: "设备分类",
              model: "typeName",
              idsStr: "typeId",
            },
            {
              type: "select",
              label: "状态",
              model: "status",
              option: [
                {
                  value: "0",
                  label: "草稿",
                },
                {
                  value: "1",
                  label: "已发布",
                },
              ],
            },
            {
              type: "input",
              label: "编号",
              model: "code",
              placeholder: "请输入",
            },
            {
              type: "select",
              label: "适用范围",
              model: "appid",
              multiple: true,
              option: this.$store.getters.appAuthList,
            },
            {
              type: "datetimerange",
              label: "创建时间",
              model: "dates",
            },
          ],
        },
        //过滤器的值
        filterFormData: {},
        //过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        actionList: [
          {
            name: "设为公共标准",
            icon: "icon-sheweigonggongbiaozhun11",
            disabled: !this.$store.getters.isBtnAuth("setPublic"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.toPublicStandard();
            },
          },
          {
            name: "新建",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("add"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.goDetailPage(row, "classificationAndSubDetail", "add");
            },
          },
          {
            name: "导入",
            icon: "icon-daoru",
            disabled: !this.$store.getters.isBtnAuth("import"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.show = true;
            },
          },
          {
            name: "导出",
            icon: "icon-daochu",
            disabled: !this.$store.getters.isBtnAuth("export"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.export();
            },
          },
          {
            name: "下载模板",
            icon: "icon-xiazai",
            disabled: !this.$store.getters.isBtnAuth("download"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.download();
            },
          },
        ],
        column: [
          {
            label: "编号",
            prop: "code",
            sortable: "custom",
          },
          {
            label: "标题",
            prop: "name",
            sortable: "custom",
          },
          {
            label: "设备分类",
            prop: "typeName",
            sortable: "custom",
          },
          {
            label: "状态",
            prop: "status",
            sortable: "custom",
            formatter: (row) => {
              return row.status == 1 ? "已发布" : "草稿";
            },
          },
          {
            label: "适用范围",
            prop: "appName",
            sortable: "custom",
          },
          {
            label: "创建单位",
            prop: "deptName",
            sortable: "custom",
          },
          {
            label: "创建人",
            prop: "createByName",
            sortable: "custom",
          },
          {
            label: "创建时间",
            prop: "createTime",
            sortable: "custom",
          },
        ],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {
            this.goDetailPage(row, "classificationAndSubDetail", "edit");
          },
        },
      },
    };
  },

  created() {
    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.$refs.klTable.getTableConfig();
    this.updateData();
  },
  methods: {
    updateData() {
      this.$refs.klTable.searchData();
    },
    // async init() {
    //   let arr = await this.$utils.getAppList();
    //   this.$set(this.tableSetting.searchObj.searchArr[5], "option", arr);
    // },
    //跳转到详情页
    goDetailPage(row, router, type) {
      const { href } = this.$router.resolve({
        name: router,
        query: {
          id: row ? row.id : "",
          // pageType: type ? type : "",
        },
      });
      window.open(href, "_blank");
    },
    // 获取表格数据
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      this.searchData = params;
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      queryClassificAndSubList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.records);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
      });
    },
    //设置为公共标准
    toPublicStandard() {
      let arr = this.$refs.klTable.getSelectData();
      let ids = [];
      if (arr.length != 0) {
        this.$confirm("确定要设为公共标准吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            arr.forEach((item) => {
              ids.push(item.id);
            });
            setCommonClassificAndSub({ ids: ids.join() }).then((res) => {
              if (res.code === 200) {
                this.$message.success(res.description);
                this.$refs.klTable.searchData();
              }
            });
          })
          .catch((e) => {});
      } else {
        this.$message.warning("请至少勾选一条数据");
      }
    },
    //导出
    export() {
      let paramStr = "";
      let params = this.searchData;
      for (const key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
          if (params[key] && params[key] != "") {
            paramStr += `${key}=${params[key]}&`;
          }
        }
      }
      paramStr = paramStr.substring(0, paramStr.length - 1);
      let path = `eam-service-standard/grade/export?${paramStr}`;
      let fileName = `分级分项验收标准`;
      this.$utils.downloadFile(null, fileName, path);
    },
    //文件上传之前
    handleChange(file) {
      let formData = new FormData();
      formData.append("file", file.raw);
      this.uploadFile = formData;
    },
    submitUpload() {
      importClassificAndSubStandard(this.uploadFile).then((res) => {
        if (typeof res.data == "string") {
          this.path = `eam-service-standard/${res.data}`;
          this.$message.error("导入失败，请下载批复文件查看");
        } else {
          this.$message.success("导入成功");
          this.show = false;
          this.updateData();
        }
      });
    },
    //点击下载批复文件
    downLoadResult() {
      this.$utils.downloadFile(null, "分级分项验收标准批复明细", this.path);
    },
    download() {
      let path = "eam-service-standard/grade/template";
      let fileName = "分级分项标准导入模板";
      this.$utils.downloadFile(null, fileName, path);
    },
  },
};
</script>
<style lang="less" scoped>
.maintenanceStandard {
  padding: 20px;
  .constentmain {
    //margin-left: 10px;
  }
}
.flowCloumn {
  display: flex;
  flex-flow: column nowrap;
}
</style>